package com.hornsun.client.personalPage.api;

import com.hornsun.domain.constant.types.InfoType;
import com.hornsun.domain.dbo.Commodity;
import com.hornsun.domain.dbo.Favourite;
import com.hornsun.domain.dbo.Information;
import com.hornsun.repository.CommodityRepository;
import com.hornsun.repository.FavouriteRepository;
import com.hornsun.repository.InformationRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/3/31.
 */
@RestController
@RequestMapping("/api/favourite")
public class FavouriteAPI {

    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private InformationRepository informationRepository;

    @Resource
    private FavouriteRepository favouriteRepository;


    /**
     * 获取收藏商品信息
     * @param httpRequest
     * @param page
     * @return
     */
    @GetMapping("/getFavouriteCommodity")
    public ResponseEntity<List<Commodity>> getFavouriteCommodity(HttpServletRequest httpRequest, @RequestParam("page") int page){
        String openid = httpRequest.getAttribute("openid").toString();
        return new ResponseEntity<List<Commodity>>(commodityRepository.getFavouriteCommodity(openid,page),HttpStatus.OK);
    }


    /**
     * 获取收藏资讯信息
     * @param httpRequest
     * @param page
     * @return
     */
    @GetMapping("/getFavouriteInformation")
    public ResponseEntity<List<Information>> getFavouriteInformation(HttpServletRequest httpRequest, @RequestParam("page") int page){
        String openid = httpRequest.getAttribute("openid").toString();
        return new ResponseEntity<List<Information>>(informationRepository.getFavouriteInformation(openid,page),HttpStatus.OK);
    }

    /**
     * 新增收藏
     * @param httpRequest
     * @param guid
     * @param type
     * @return
     */
    @Transactional
    @PostMapping("/addFavourite")
    public ResponseEntity<?> addFavourite(HttpServletRequest httpRequest,@RequestParam("guid") String guid,@RequestParam("type")int type){
        String openid = httpRequest.getAttribute("openid").toString();
        if(favouriteRepository.checkFavourite(openid,guid)){
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        if (type== InfoType.INFORMATION.getType()){
            informationRepository.updateFavourite(guid);
        }
        if (type==InfoType.COMMODITY.getType()){
            commodityRepository.updateFavourite(guid);
        }
        Favourite favourite = new Favourite();
        favourite.setOpenid(openid);
        favourite.setType((byte)type);
        favourite.setCollectContentGuid(guid);
        favourite.setCollectTime(new Date());
        favourite.setFavouriteGuid(UUID.randomUUID().toString());
        favouriteRepository.insertFavourite(favourite);
     return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 取消收藏
     * @param httpRequest
     * @param guid
     * @return
     */
    @Transactional
    @PostMapping("/cancelFavourite")
    public ResponseEntity<?> cancelFavourite(HttpServletRequest httpRequest,@RequestParam("guid") String guid){
        String openid = httpRequest.getAttribute("openid").toString();
        favouriteRepository.deleteFavourite(openid,guid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }




}
